﻿
using PEUtils;

namespace Common
{
    public class ProcessLauncher<T> where T : Process<T>, new()
    {
        ConsoleCommand cmd;
        string cmdArgs = "";

        const int defaultID = 1001;

        public ProcessLauncher(T process, string[] args)
        {
            int id = defaultID;
            if (args.Length == 0)
            {
                while (true)
                {
                    Console.WriteLine($"输入进程ID: (按下回车将使用默认进程ID:{defaultID}启动)");
                    string? input = Console.ReadLine();
                    // 使用默认ID
                    if (input == string.Empty)
                        break;
                    // 使用指定ID
                    if (int.TryParse(input, out id))
                        break;

                    Console.WriteLine($"输入无效: {input}");
                }
            }

            // 初始化进程日志
            LogConfig logConfig = new()
            {
                loggerEnum = LoggerType.Console,
                saveName = $"{typeof(T).Name}_{id}.txt",
            }; 
            PELog.InitSettings(logConfig);

            process.ProcessID = id;
            PELog.LogYellow($"{typeof(T).Name} 进程启动成功, 进程id: {process.ProcessID}");

            // 开个线程处理控制台命令
            Task.Run(() =>
            {
                while (true)
                {
                    switch (cmd)
                    {
                        case ConsoleCommand.quit:
                            Thread.Sleep(1000);
                            continue;
                        default:
                            string? input = Console.ReadLine();
                            // Console.WriteLine(input);
                            if (!string.IsNullOrEmpty(input))
                            {
                                // 解析命令
                                string[] cmdArr = input.Split(' ');
                                try
                                {
                                    // Console.WriteLine($"{cmdArr[0]}");
                                    ConsoleCommand cmd1 = (ConsoleCommand)Enum.Parse(typeof(ConsoleCommand), cmdArr[0]);
                                    // Console.WriteLine($"{cmd1}");
                                    if (cmd1 < ConsoleCommand.max)
                                    {
                                        cmd = cmd1;
                                        if (cmdArr.Length > 1)
                                            cmdArgs = cmdArr[1];
                                    }
                                }
                                catch (Exception e)
                                {
                                    PELog.Warn($"命令不存在: {input}");
                                    cmd = ConsoleCommand.none;
                                    cmdArgs = "";
                                }
                            }
                            break;
                    }
                }
            });

            // 主循环逻辑
            process.Awake();
            while (true)
            {
                process.Tick();
                Thread.Sleep(30);

                if (cmd == ConsoleCommand.quit)
                {
                    process.Destroy();
                    break;
                }

                process.HandleConsoleCommand(cmd, cmdArgs);
                cmd = ConsoleCommand.none;
                cmdArgs = string.Empty;
            }
        }
    }
}
